package com.fit.pages.coach;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.fit.annotations.AdminAccess;
import com.fit.dao.coach.CoachDAO;
import com.fit.entities.coach.Coach;
import com.fit.model.MD5;
import com.fit.pages.ErrorPage;

@AdminAccess
public class Edit {

	@Property
	@Persist
	private Coach coach;
	@Inject
	private CoachDAO coachDAO;
	@InjectPage
	private ErrorPage errorPage;
	@Property
	private int newAge;

	Object onActivate(Long id) {
		coach = coachDAO.findByID(id);
		if (coach == null) {
			errorPage.setMessage("Greska prilikom prikaza zapisa korisnika! ");
			return errorPage;
		}
		return null;
	}

	Object onSuccess() {
		try {

			if (newAge != 0) {
				
				if (newAge == coach.getAge()) {
					coach.setAge(newAge);
				}
			}
			coachDAO.update(coach);
			return Browse.class;
		} catch (Exception e) {
			errorPage.setMessage(" Error while ");
			return errorPage;
		}

	}

}
